(function ($, window) {
    var dataManager = window.dataManager = {};

    dataManager.set = function (name) {
        return function (value) {
            value = typeof value === 'object' ? JSON.stringify(value) : value;
//          console.log('[setValue] ' + name + '=' + value);
            return window.localStorage.setItem(name, value);
        }
    };

    /**
     * 从localStorage中获取数据
     * @param name 字段名称
     * @param isObject 是否需要转成object
     * @returns {Function}
     */
    dataManager.get = function (name, isObject) {
        return function () {
//            console.log('[getValue] ' + name);
            var value = window.localStorage.getItem(name);
            return isObject ? JSON.parse(value) : value;
        }
    };

    dataManager.isLogin = function () {
        var userName = plus.storage.getItem("userName");
        return !!userName;
    };

    dataManager.logout = function () {
    		plus.storage.setItem('userName', '');
        plus.storage.removeItem("userName");
    };
    
    //用户更新app之后，需要执行一次logout，不然登录时会返回casurl=“”
    //flag为1，表示已经执行过logout
    dataManager.getAutoLogoutFlag = function() {
    		return plus.storage.getItem('autoLogoutFlag');
    };
    
    dataManager.setAutoLogoutFlag = function(flag) {
    		return plus.storage.setItem('autoLogoutFlag',flag);
    };

    dataManager.setUserName = function (userName) {
        plus.storage.setItem('userName', userName);
    };

    dataManager.getUserName = function () {
        return plus.storage.getItem("userName");
    };
    
    dataManager.setSession = function (session) {
        plus.storage.setItem('session', session);
    };

    dataManager.getSession = function () {
        return plus.storage.getItem("session");
    };
    
    dataManager.setCurrentScenarioType = function(scenarioType){
    		plus.storage.setItem('scenarioType', scenarioType);
    };
    
    dataManager.getCurrentScenarioType = function(){
    		return plus.storage.getItem('scenarioType');
    };
    
    dataManager.setLastLoginUserName = function (userName){
    		plus.storage.setItem('lastLoginuserName', userName);
    };
    dataManager.getLastLoginUserName = function (){
    		return plus.storage.getItem("lastLoginuserName");
    };
//	dataManager.setUserInfo = function(UserInfo){
//		console.log("userInfo");
//		plus.storage.setItem("UserInfo", JSON.stringify(UserInfo));
//	}
//	
//	dataManager.getUserInfo = function(){
//		var userInfo = plus.storage.getItem("UserInfo");
//		if (userInfo) {
//          return JSON.parse(userInfo);
//      } else {
//          return undefined;
//      }
//	}

	//flag 0没有显示过  1显示过了
	dataManager.setGuideFlag = function(flag){
		window.localStorage.setItem("guideFlag",flag);
	};
	
	dataManager.getGuideFlag = function(){
		return window.localStorage.getItem("guideFlag") || 0;
	};

    dataManager.setLang = function (lang) {
        window.localStorage.setItem('lang', lang);
    };

    dataManager.getLang = function () {
        return window.localStorage.getItem('lang') || 'cn';
    };

    dataManager.getLocale = function () {
        return window.locale[dataManager.getLang()];
    };

    dataManager.getDevSN = function () {
        return window.localStorage.getItem('sn');
    };

    dataManager.setEventsList = function (eventsList) {
        window.localStorage.setItem('eventsList', eventsList);
    };

    dataManager.getEventsList = function () {
        return window.localStorage.getItem('eventsList');
    }
}(mui, window));